<?php

namespace App\Admin\Controllers;

use App\Models\OfficeArea;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UsersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'));
        $grid->column('openid', __('Openid'));
        $grid->column('avatarurl', __('Avatarurl'))->display(function ($av){
            $text = '<img style="width:100px;height:100px;" src='."$av".' />';
            return $text;
        });
        $grid->column('email', __('Email'));
//        $grid->column('password', __('Password'));
//        $grid->column('token', __('Token'));
//        $grid->column('type', __('Type'));
        //打卡方式
        $arr = [1 => '办公室', 2 => '远程    ', 3 => '通用'];
        $grid->column('type','打卡方式')->editable('select', $arr);
        //打卡区域
        $areaarr = [];
        $get_areas = OfficeArea::where([
            ['status',1]
        ])
            ->get()
            ->toArray();

        if($get_areas){
            foreach ($get_areas as $k=>$v){
                $areaarr[$get_areas[$k]['id']] = $get_areas[$k]['name'];
            }
        }

        $grid->column('area_id','打卡区域')->editable('select', $areaarr);
//        $grid->column('status', __('Status'));
        // 设置text、color、和存储值
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
        ];
        $grid->column('status','就职状态')->switch($states);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->disableCreateButton();

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('openid', __('Openid'));
        $show->field('avatarurl', __('头像'));
        $show->field('email', __('Email'));
        $show->field('password', __('Password'));
        $show->field('token', __('Token'));
        $show->field('type', __('Type'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->text('openid', __('Openid'));
        $form->text('avatarurl', __('Avatarurl'));
        $form->email('email', __('Email'));
        $form->password('password', __('Password'));
        $form->text('token', __('Token'));
        $form->number('type', __('Type'))->default(1);
        $form->number('area_id', __('area_id'))->default(1);
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
        ];
        $form->switch('status', __('Status'))->options($states);

        return $form;
    }

}
